package server.startup;

import org.apache.commons.digester3.Digester;
import server.*;
import server.core.StandardContext;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;


public final class ContextConfig implements LifecycleListener {
    //静态变量及方法-----------------------------------------------------------------------------
    private static Digester webDigester = createWebDigester();
    private static Digester createWebDigester() {
        URL url = null;
        Digester webDigester = new Digester();
        webDigester.setValidating(true);

        url = ContextConfig.class.getResource(Constants.WebDtdResourcePath_22);
        webDigester.register(Constants.WebDtdPublicId_22, url.toString());

        url = ContextConfig.class.getResource(Constants.WebDtdResourcePath_23);
        webDigester.register(Constants.WebDtdPublicId_23, url.toString());

        webDigester.addRuleSet(new WebRuleSet());
        return webDigester;
    }

    //实例变量及方法-----------------------------------------------------------------------------
    //关联的应用容器
    private Context context = null;
    private int debug = 0;
    private boolean ok = false;

    private void log(String message) {
        Logger logger = null;
        if (context != null)
            logger = context.getLogger();
        if (logger != null)
            logger.log("ContextConfig[" + context.getName() + "]: " + message);
        else
            System.out.println("ContextConfig[" + context.getName() + "]: " + message);
    }
    private void log(String message, Throwable throwable) {
        Logger logger = null;
        if (context != null)
            logger = context.getLogger();
        if (logger != null)
            logger.log("ContextConfig[" + context.getName() + "] " + message, throwable);
        else {
            System.out.println("ContextConfig[" + context.getName() + "]: " + message);
            System.out.println("" + throwable);
            throwable.printStackTrace(System.out);
        }

    }
    private synchronized void stop() {

    }
    //开始配置Context--------------------------------------------------------------------------------
    private synchronized void start() {
        if (debug > 0)
            log("ContextConfig: Processing START");
        context.setConfigured(true);
        ok = true;


    }

    private void defaultConfig() {
        File file = new File(System.getProperty("server.base"), Constants.DEFAULT_WEB_XML);
        FileInputStream fis = null;
        try {
            //尝试文件是能正常打开关闭
            fis = new FileInputStream(file);
            fis.close();
        } catch (IOException e) {
            log("没有默认配置文件,将只使用应用配置文件");
            return;
        }
//        synchronized ()

    }
    //实现LifecycleListener接口----------------------------------------------------------------------
    @Override
    public void lifecycleEvent(LifecycleEvent event) {
        try {
            context = (Context)event.getLifecycle();
            if (context instanceof StandardContext) {
                int contextDebug = ((StandardContext) context).getDebug();
                if (contextDebug > debug)
                    debug = contextDebug;
            }
        } catch (ClassCastException e) {
            log("监听器[ContextConfig]监听的{" + event.getLifecycle() + "}不是Context容器!");
            return;
        }
        if (Lifecycle.START_EVENT.equals(event.getType())) {
            start();
        } else if (Lifecycle.STOP_EVENT.equals(event.getType()))
            stop();
    }
}
